package com.walkline.screen;

import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

import com.walkline.travelassistant.inf.Direction;
import com.walkline.travelassistant.inf.Route;
import com.walkline.travelassistant.inf.Scheme;
import com.walkline.travelassistant.inf.Step;
import com.walkline.util.Function;
import com.walkline.util.ui.ForegroundManager;
import com.walkline.util.ui.ListStyleButtonField;
import com.walkline.util.ui.ListStyleButtonSet;

public final class ShowQueryResultScreen extends MainScreen
{
	private ForegroundManager _foreground = new ForegroundManager(0);
	private ListStyleButtonSet _resultSet = new ListStyleButtonSet(false);
	private ListStyleButtonSet _taxiSet = new ListStyleButtonSet(false);

    public ShowQueryResultScreen(final Direction direction)
    {
    	super (NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);

    	add(_foreground);

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				refreshDirectionResultList(direction);
			}
		});
    }

    public ShowQueryResultScreen(final Scheme scheme)
    {
    	super (NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);

    	add(_foreground);

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				refreshSchemeResultList(scheme);
			}
		});
    }

    private void refreshDirectionResultList(Direction direction)
	{
		final Vector routeList;
		Route route = null;
		Scheme scheme = null;
		Step step;
		Vector stepWrapper;
		ListStyleButtonField item;

		if (direction.getRoutes().hasScheme())
		{
			routeList = direction.getRoutes().getSchemes();

			for (int i=0; i<routeList.size(); i++)
			{
				scheme = (Scheme) routeList.elementAt(i);
				item = new ListStyleButtonField(scheme);
				item.setChangeListener(new FieldChangeListener()
				{
					public void fieldChanged(Field field, int context)
					{
						UiApplication.getUiApplication().pushScreen(new ShowQueryResultScreen(((ListStyleButtonField) _resultSet.getFieldWithFocus()).getScheme()));
					}
				});
				_resultSet.add(item);
			}
		} else {
			routeList = direction.getRoutes().getRoutes();
			route = (Route) routeList.elementAt(0);
			stepWrapper = route.getSteps().getSteps();

			_foreground.add(new LabelField(route.getDistanceStr()));

			item = new ListStyleButtonField("起点：" + direction.getRoutes().getOriginName());
			_resultSet.add(item);
			for (int i=0; i<stepWrapper.size(); i++)
			{
				step = (Step) stepWrapper.elementAt(i);
				if (step != null)
				{
					item = new ListStyleButtonField(step);
					item.setChangeListener(new FieldChangeListener()
					{
						public void fieldChanged(Field field, int context)
						{
							if (context != FieldChangeListener.PROGRAMMATIC)
							{
								UiApplication.getUiApplication().pushScreen(new ShowStaticImageScreen(((ListStyleButtonField)_resultSet.getFieldWithFocus()).getPath()));
							}
						}
					});

					_resultSet.add(item);
				}
			}
			item = new ListStyleButtonField("终点：" + direction.getRoutes().getDestinationName());
			_resultSet.add(item);
		}

		if (direction.getTaxi() != null)
		{
			item = new ListStyleButtonField(direction.getTaxi());
			item.setChangeListener(new FieldChangeListener()
			{
				public void fieldChanged(Field field, int context)
				{
					if (context != FieldChangeListener.PROGRAMMATIC)
					{
						Function.errorDialog(((ListStyleButtonField) _taxiSet.getFieldWithFocus()).getTaxiRemark());
					}
				}
			});
			_taxiSet.add(item);			
		}

		if (_resultSet.getManager() == null) {_foreground.add(_resultSet);}
		if (_taxiSet.getManager() == null) {_foreground.add(_taxiSet);}
	}

    private void refreshSchemeResultList(Scheme scheme)
	{
		Step step;
		Vector stepWrapper;
		ListStyleButtonField item;

		stepWrapper = scheme.getSteps().getSteps();

		_foreground.add(new LabelField(scheme.getDistanceStr()));

		item = new ListStyleButtonField("路线：" + scheme.getSchemeName());
		_resultSet.add(item);
		for (int i=0; i<stepWrapper.size(); i++)
		{
			step = (Step) stepWrapper.elementAt(i);
			if (step != null)
			{
				item = new ListStyleButtonField(step);
				item.setChangeListener(new FieldChangeListener()
				{
					public void fieldChanged(Field field, int context)
					{
						if (context != FieldChangeListener.PROGRAMMATIC)
						{
						}
					}
				});

				_resultSet.add(item);
			}
		}

		if (_resultSet.getManager() == null) {_foreground.add(_resultSet);}
	}
}